package com.example.yls.demo5;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements OnItemDelClickListener,OnItemEditClickListener {
    private RecyclerView rlvStu;
    private ArrayList<Student> studentArrayList = new ArrayList<>();
    private ArrayList<Student> searchArrayList = new ArrayList<>();
    private StuAdapter adapter;
    private Button btnAddStu;
    private EditText editKeyword;
    private Button btnSearch;
    private boolean isResultList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initStudentArrayList();

        adapter = new StuAdapter(MainActivity.this, studentArrayList,/*this.searchArrayList,*/MainActivity.this,MainActivity.this,isResultList);

        rlvStu = findViewById(R.id.rlv_stu);
        rlvStu.setAdapter(adapter);

        LinearLayoutManager manager = new LinearLayoutManager(MainActivity.this);
        rlvStu.setLayoutManager(manager);

        initAddStu();

        initSearchView();



    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.e("MainActivity", "requestCode = " + requestCode + "resultCode = " + resultCode);

        if (data == null) {
            return;
        }

        if (resultCode == 2001) {
            String name = data.getStringExtra("NAME");
            int age = data.getIntExtra("AGE", 0);
            int imgId = data.getIntExtra("IMG", R.drawable.img1);

            Student s = new Student("10086", name, age, imgId);
            studentArrayList.add(s);
            adapter.notifyDataSetChanged();
        } else if (resultCode == 3001) {
            String name = data.getStringExtra("NAME");
            int age = data.getIntExtra("AGE", 0);
            int imgId = data.getIntExtra("IMG", R.drawable.img6);
            String no = data.getStringExtra("NO");

            for (int i = 0; i<studentArrayList.size(); i++){
                Student s =studentArrayList.get(i);

                if (s.getNo().equals(no)) {
                    s.setName(name);
                    s.setAge(age);
                    s.setImgId(imgId);

                    /*adapter.notifyDataSetChanged();*/
                }
            }

            String keyword = editKeyword.getText().toString().trim();
            //使用学生列表和关键字 得到搜索结果列表
            search(studentArrayList, keyword);
            adapter.changeData(searchArrayList);
            isResultList = true;

        }
    }


    /**
     * 初始化学生列表
     */
    private void initStudentArrayList() {
        Student s1 = new Student("10081", "周杰伦", 35, R.drawable.img1);
        Student s2 = new Student("10082", "陈奕迅", 34, R.drawable.img2);
        Student s3 = new Student("10083", "刘德华", 37, R.drawable.img3);
        Student s4 = new Student("10084", "张学友", 38, R.drawable.img4);
        Student s5 = new Student("10085", "渣渣辉", 36, R.drawable.img5);
        studentArrayList.add(s1);
        studentArrayList.add(s2);
        studentArrayList.add(s3);
        studentArrayList.add(s4);
        studentArrayList.add(s5);

    }

    /**
     * 添加学生
     */
    private void initAddStu() {
        btnAddStu = findViewById(R.id.btn_addStu);
        btnAddStu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, Addactivity.class);
                startActivityForResult(intent, 1001);
            }
        });

    }

    /**
     * 删除学生
     */
    @Override
    public void del(int i) {
        Log.e("MainActivity", " del " + i);
        studentArrayList.remove(i);
        adapter.notifyDataSetChanged();
    }

    /**
     * 初始化搜索模块
     */
    private void initSearchView(){
        editKeyword = findViewById(R.id.edt_keyword);
        btnSearch = findViewById(R.id.btn_search);

        btnSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String keyword = editKeyword.getText().toString().trim();
                //使用学生列表和关键字 得到搜索结果列表
                search(studentArrayList, keyword);
                adapter.changeData(searchArrayList);
                isResultList=true;
            }
        });

        editKeyword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String keyword = editKeyword.getText().toString().trim();
                // 使用学生列表和关键字 得到搜索结果列表
                search(studentArrayList, keyword);
                adapter.changeData(searchArrayList);
                isResultList=true;
                initResultList();
            }
        });

    }

    /**
     * 使用学生列表和关键字 得到搜索结果列表
     * @param studentArrayList
     * @param keyword
     */
    private void search(ArrayList<Student> studentArrayList,String keyword){
        searchArrayList.clear();
        for (int i = 0; i<studentArrayList.size(); i++){
            Student s = studentArrayList.get(i);
            //如果学生姓名包含了关键字 这个学生就加入到结果列表
            if (s.getName().contains(keyword)){
                searchArrayList.add(s);
            }
            //如果学生年龄等于关键字 这个学生就加入到结果列表
            else if (String .valueOf(s.getAge()).equals(keyword)){
                searchArrayList.add(s);
            }
        }
    }

    /**
     * 编辑学生信息
     */
    @Override
    public void Edit(final int i, View view) {
        Student s = studentArrayList.get(i);

        //解决搜索后学生点击显示错位的问题
        if (isResultList){
            s = searchArrayList.get(i);
        }


        final Student finalS = s;
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent();
                intent.setClass(MainActivity.this,EditActivity.class);
                intent.putExtra("NAME", finalS.getName());
                intent.putExtra("AGE", finalS.getAge());
                intent.putExtra("NO", finalS.getNo());
                startActivityForResult(intent,1002);
            }
        });
    }

    public boolean initResultList(){
        return isResultList;
    }

}
